import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/Dashboard.vue"),
        name: "Dashboard",
        meta: {
          title: "首页工作台",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: '/dataset',
    component: Layouts,
    children: [
      {
        path: 'dataset-list',
        name: 'DatasetList',
        component: () => import('@/views/DatasetList.vue'),
        meta: { title: '数据集管理', svgIcon: 'dataset' }
      }
    ]
  },
  {
    path: '/report',
    name: 'ReportCenter',
    component: Layouts,
    children: [
      {
        path: 'multi-dim-report',
        name: 'MultiDimReport',
        component: () => import('@/views/report/MultiDimReport.vue'),
        meta: { title: '报表设计与管理', svgIcon: 'report-center' }
      },
      {
        path: 'report-designer/:id?',
        name: 'ReportDesigner',
        component: () => import('@/views/report/ReportDesigner.vue'),
        meta: { title: '拖拽报表设计器', hidden: true }, // hidden: true 用于菜单不显示，仅编程式跳转
        props: true
      },
      {
        path: 'report-preview/:id',
        name: 'ReportPreview',
        component: () => import('@/views/report/ReportPreview.vue'),
        meta: { title: '报表预览', hidden: true },
        props: true
      }
    ]
  },
  {
    path: '/screen',
    name: 'ScreenCenter',
    component: Layouts,
    children: [
      {
        path: 'screen-list',
        name: 'ScreenList',
        component: () => import('@/views/screen/ScreenList.vue'),
        meta: { title: '大屏可视化管理', svgIcon: 'screen-center' }
      },
      {
        path: 'designer/:id?',
        name: 'ScreenDesigner',
        component: () => import('@/views/screen/ScreenDesigner.vue'),
        meta: { title: '大屏设计器', hidden: true },
        props: true
      },
      {
        path: 'preview/:id',
        name: 'ScreenPreview',
        component: () => import('@/views/screen/ScreenPreview.vue'),
        meta: { title: '大屏预览', hidden: true },
        props: true
      }
    ]
  },
  {
    path: '/export',
    name: 'ExportCenter',
    component: Layouts,
    meta: { title: '分享与导出中心', svgIcon: 'export-center' },
    children: [
      {
        path: 'report-export',
        name: 'ReportExport',
        component: () => import('@/views/export/ReportExport.vue'),
        meta: { title: '报表导出与分享' }
      },
      {
        path: 'screen-export',
        name: 'ScreenExport',
        component: () => import('@/views/export/ScreenExport.vue'),
        meta: { title: '大屏导出与分享' }
      },
      {
        path: 'import-export',
        name: 'ImportExport',
        component: () => import('@/views/export/ImportExport.vue'),
        meta: { title: '配置文件导入与导出' }
      }
    ]
  },
  {
    path: '/user',
    name: 'UserCenter',
    component: Layouts, // 你的主布局组件
    meta: { title: '用户与角色管理', svgIcon: 'user-center' },
    children: [
      {
        path: 'user-manage',
        name: 'UserManage',
        component: () => import('@/views/user/UserManage.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'role-manage',
        name: 'RoleManage',
        component: () => import('@/views/user/RoleManage.vue'),
        meta: { title: '角色管理' }
      }
    ]
  },
  {
    path: '/datasource',
    name: 'DataSourceCenter',
    component: Layouts, 
    meta: { title: '数据源管理与动态绑定', svgIcon: 'data-source' },
    children: [
      {
        path: 'list',
        name: 'DataSourceManage',
        component: () => import('@/views/datasource/DataSourceManage.vue'),
        meta: { title: '数据源管理' }
      },
      {
        path: 'binding',
        name: 'DataSourceBinding',
        component: () => import('@/views/datasource/DataSourceBinding.vue'),
        meta: { title: '动态数据绑定' }
      }
    ]
  }

]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [

]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
